package com.jumper.study.config;

import java.util.concurrent.Executor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 创建异步工作线程池
 */
@Configuration
@EnableAsync
public class AsyncConfig {
    @Value("${study-setting.async.corePoolSize:5}")
    private Integer corePoolSize;

    @Value("${study-setting.async.maxPoolSize:10}")
    private Integer maxPoolSize;

    @Value("${study-setting.async.queueCapacity:100}")
    private Integer queueCapacity;

    @Bean("async")
    public Executor threadPoolExecutor() {
        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setCorePoolSize(corePoolSize);
        pool.setMaxPoolSize(maxPoolSize);
        pool.setQueueCapacity(queueCapacity);
        pool.setWaitForTasksToCompleteOnShutdown(true);
        pool.initialize();

        return pool;
    }
}
